﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Composite.Regions;
using EQual.Infrastructure;

namespace EQual.MainModule.Employees.Proposals
{
 
    public class ProposalsController : ModulesControllerBase<ProposalsGridView,DataAccesLayer .Proposal >
    {
        IRegionManager _regionManager;

        public ProposalsController(IRegionManager regionManager)
            : this()
        {
            this._regionManager = regionManager;
        }

        public ProposalsController()
        {
            this.AccesLayer = new DataAccesLayer.EQualDataLayerFactory();
            InitializeViews();
        }

        #region IModuleController Members

        public override void Run()
        {
            View = new ProposalsGridView();

            View.Model = new { ItemsCollection = AccesLayer.GetEmployeeAccesLayer().GetProposals() };

            View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);
            View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemSelected);

            this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => this.View);
            this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
        }

        void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.ActiveItem = e.Value as DataAccesLayer .Proposal ;
        }

        void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.AccesLayer.GetEmployeeAccesLayer ().AddNewProposal(e.Value);
        }

        #endregion

        public override DataAccesLayer.Proposal  ActiveItem
        {
            get
            {
                return base.ActiveItem;
            }
            set
            {
                base.ActiveItem = value;
            }
        }

        public override void Dispaly(DataAccesLayer.Proposal item)
        {
            InjectViews();


            base.Dispaly(item);
        }

        EQual.MainModule.DetailsContainer containerView = new DetailsContainer();

        protected virtual void InjectViews()
        {
            //throw in to the datails region the datails view
            IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

            if (!region.Views.Contains(containerView))
            {
               // containerView.AddView(GetDecoratedView(trainingPresenter.View, "Συμετέχοντες"));
              //  containerView.AddView(GetDecoratedView(detailsView, ""));

                region.Add(containerView);
            }

            //activate the current view
            region.Activate(containerView);
        }
    }

}
